package com.bigknow.appstore.web.base.controller;

import com.bigknow.appstore.web.app.service.impl.AppStoreService;
import com.bigknow.appstore.web.app.service.impl.MyAppService;
import com.bigknow.appstore.web.framework.controller.BaseController;
import org.mongodb.morphia.Datastore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**p
 * Created by zhangwei on 2016/10/17.
 */
@Controller
@RequestMapping(value = "/myApp")
public class MyAppController extends BaseController {

    @Autowired
    private MyAppService myAppService;

    @Autowired
    private AppStoreService appStoreService;

    @Autowired
    private Datastore ds;

    @RequestMapping("/favorite")
    @ResponseBody
    public boolean favorite(String appId, boolean favorite) {
        return myAppService.favorite(appId, favorite);
    }

    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView view = new ModelAndView("/app/myApps");
        view.addObject("isLogin", appStoreService.isLogin());
        view.addObject("userName", appStoreService.getUser());
        view.addObject("recommendApps", appStoreService.getRecommendApps());
        view.addObject("top10", appStoreService.top(10));
        view.addObject("tags", appStoreService.getAllTags());
        return view;
    }

    @RequestMapping("/favoriteIndex")
    public ModelAndView favoriteApps() {
        ModelAndView view = new ModelAndView("/app/favoriteApps");
        view.addObject("isLogin", appStoreService.isLogin());
        view.addObject("userName", appStoreService.getUser());
        view.addObject("recommendApps", appStoreService.getRecommendApps());
        view.addObject("top10", appStoreService.top(10));
        view.addObject("tags", appStoreService.getAllTags());
        return view;
    }

    @RequestMapping("/appList")
    public ModelAndView getMyApps(String[] tagIds, String orderType, String state) {
        ModelAndView view = new ModelAndView("/app/appList");
        view.addObject("apps", myAppService.getMyAppsByCondition(tagIds, orderType,state));
        return view;
    }

    @RequestMapping("/favoriteApps")
    public ModelAndView getFavoriteApps(String[] tagIds, String orderType) {
        ModelAndView view = new ModelAndView("/app/appList");
        view.addObject("apps", myAppService.getFavoriteAppsByCondition(tagIds, orderType));
        return view;
    }

    @RequestMapping("/apply")
    @ResponseBody
    public boolean apply(String[] dataNodeIds, String appId) {
        myAppService.applySubscriber(dataNodeIds,appId);
        return true;
    }
}
